import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_item_acknowledgement_status.dart';
import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_info.dart';
import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_info_cancellation_info.dart';
import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_info_pause_info.dart';
import 'package:backend.purchases/domain/entities/google_play_api/google_play_api_purchased_item.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_purchased_subscription_info_cancellation_info_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_purchased_subscription_info_pause_info_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_purchased_subscription_info_price_change_status_from_api_one.dart';

abstract class UpdateGooglePlayPurchasedSubscriptionInfoFromApiOne {
  GooglePlayPurchasedSubscriptionInfo call({
    required GooglePlayPurchasedSubscriptionInfo purchasedSubscriptionInfo,
    required GooglePlayApiPurchasedSubscription updatedApiPurchasedSubscription,
  });
}

class UpdateGooglePlayPurchasedSubscriptionInfoFromApiOneImpl
    implements UpdateGooglePlayPurchasedSubscriptionInfoFromApiOne {
  const UpdateGooglePlayPurchasedSubscriptionInfoFromApiOneImpl({
    required CreateGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne
        createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne,
    required CreateGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne
        createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne,
    required CreateGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne
        createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne,
  })  : _createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne =
            createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne,
        _createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne =
            createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne,
        _createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne =
            createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne;

  final CreateGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne
      _createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne;
  final CreateGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne
      _createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne;
  final CreateGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne
      _createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne;

  @override
  GooglePlayPurchasedSubscriptionInfo call({
    required GooglePlayPurchasedSubscriptionInfo purchasedSubscriptionInfo,
    required GooglePlayApiPurchasedSubscription updatedApiPurchasedSubscription,
  }) {
    GooglePlayPurchasedSubscriptionInfoCancellationInfo? updatedCancellationInfo;

    if (updatedApiPurchasedSubscription.cancellationInfo != null) {
      updatedCancellationInfo =
          _createGooglePlayPurchasedSubscriptionInfoCancellationInfoFromApiOne(
        apiCancellationInfo: updatedApiPurchasedSubscription.cancellationInfo!,
      );
    }

    GooglePlayPurchasedSubscriptionInfoPauseInfo? updatedPauseInfo;

    if (updatedApiPurchasedSubscription.pauseInfo != null) {
      updatedPauseInfo = _createGooglePlayPurchasedSubscriptionInfoPauseInfoFromApiOne(
        apiPauseInfo: updatedApiPurchasedSubscription.pauseInfo!,
      );
    }

    final updatedPriceChangeStatus =
        _createGooglePlayPurchasedSubscriptionInfoPriceChangeStatusFromApiOne(
      apiPriceChangeStatus: updatedApiPurchasedSubscription.priceChangeStatus,
    );

    var updatedAcknowledgementStatus = purchasedSubscriptionInfo.acknowledgementStatus;

    if (updatedApiPurchasedSubscription.acknowledged) {
      updatedAcknowledgementStatus = GooglePlayPurchasedItemAcknowledgementStatus.acknowledged;
    }

    return purchasedSubscriptionInfo.copyWith(
      autoRenewalEnabled: () => updatedApiPurchasedSubscription.autoRenewalEnabled,
      acknowledgementStatus: () => updatedAcknowledgementStatus,
      cancellationInfo: () => updatedCancellationInfo,
      pauseInfo: () => updatedPauseInfo,
      priceChangeStatus: () => updatedPriceChangeStatus,
    );
  }
}
